package m.html.form;

import m.html.element.Element;
import m.html.element.Text;
import m.html.element.other.IElement;

import java.util.*;

public class CustomForm implements IForm {
    private final boolean showHead;
    private final String title;//标题
    private String subTitle="";//子标题
    private String test;
    private String mounted;
    private boolean isBlock=false;
    private final List<IElement> eles=new ArrayList<>();//内容
    private final List<FormButton> buttons=new ArrayList<>();//按钮项
    private final List<FormEvent> events=new ArrayList<>();
    private final Map<String,Object> dataMap=new HashMap<>();
    private final Map<String,String> watchMap=new HashMap<>();
    public CustomForm(String title){
        this(title,true);
    }
    public CustomForm(String title,boolean showHead){
        this.title=title;
        this.showHead=showHead;
    }
    public CustomForm(IElement... item){
        this("",false);
        addElement(item);
        isBlock=true;
    }

    @Override
    public boolean isShowHead() {
        return this.showHead;
    }

    /** 子标题
     * 支持标签和表达式
     */
    public CustomForm setSubTitle(String subTitle) {
        this.subTitle = subTitle;
        return this;
    }

    /**
     * 设置表达式 显示或隐藏
     */
    public CustomForm setTest(String ifTest){
        this.test=ifTest;
        return this;
    }
    /**
     * 添加内容项
     */
    public CustomForm addElement(IElement... item){
        this.eles.addAll(Arrays.asList(item));
        return this;
    }

    /**
     * 添加按钮
     */
    public CustomForm addButton(FormButton... button){
        this.buttons.addAll(Arrays.asList(button));
        return this;
    }

    /**
     * 设置页面加载后执行的js
     */
    public CustomForm setMounted(String mounted) {
        this.mounted = mounted;
        return this;
    }

    /**
     * 添加表单数据
     */
    public CustomForm addData(String key, Object data){
        this.dataMap.put(key,data);
        return this;
    }
    /**
     * 添加属性监听
     */
    public CustomForm addWatch(String dataKey,String js){
        this.watchMap.put(dataKey,js);
        return this;
    }

    /**
     * 添加方法
     */
    public CustomForm addMethod(FormEvent... event){
        this.events.addAll(Arrays.asList(event));
        return this;
    }

    @Override
    public IElement getElement(boolean hasBack) {
        if(isBlock){
            return Element.tag("div").E(eles.toArray(new IElement[0]));
        }
        Element card=Element.tag("a-card").A("hoverable").A(":bodyStyle","{padding:0}")
                .E(eles.toArray(new IElement[0]));
        if(!showHead&&!hasBack) return card;
        Element ele=new Element("a-page-header");
        ele
        .E(
            Element.tag("template").A("#title").E(new Text(this.title))
        )
        .E(
            Element.tag("template").A("#subTitle").E(new Text(this.subTitle))
        )
        .E(
            Element.tag("template").A("#extra")
            .E(this.buttons)
            .E(
                hasBack?Element.tag("m-back").E(new Text("{{param.openType=='PAGE'?'返回':'关闭'}}")).setExtend(" v-if=\"param.isOpen\" @click=\"back(false);\" "):null
            )
        )
        .E(card);
        return ele;
    }

    @Override
    public String getMounted() {
        return mounted;
    }
    @Override
    public Map<String,Object> getData() {
        return this.dataMap;
    }

    @Override
    public List<FormEvent> getMethods() {
        return this.events;
    }
    @Override
    public Map<String, String> getWatch() {
        return this.watchMap;
    }

    @Override
    public String getTest() {
        return test;
    }
}
